-- 分裂投射

local MissionWeapon = TaleWorlds.MountAndBlade.MissionWeapon

local onlyPlayer = true
local split_amount = 3
--local agents = CreateAgentList()
local behavior

local function degreesToRadians(degrees)
    return degrees * (math.pi / 180)
end

return {
onBeforeMissionBehaviorInitialize = function (mission)
    behavior = SubModule:CreateMissionBehavior({
        -- onMissileHit = function (attacker, victim, isCanceled, collisionData)
        --     if onlyPlayer and not attacker.IsMainAgent then
        --         return
        --     end
        --     if agents:Contains(attacker) then
        --         return
        --     end

        --     local missile = GetMissile(collisionData.AffectorWeaponSlotOrMissileIndex)
        --     if missile == nil then
        --         return
        --     end

        --     local weapon = missile.Weapon
        --     if weapon == MissionWeapon.Invalid or weapon.CurrentUsageItem == nil then
        --         return
        --     end

        --     InformationManager.DisplayMessage(InformationMessage("IsConsumable: "..tostring(weapon.CurrentUsageItem.IsConsumable), Colors.Cyan))
        --     InformationManager.DisplayMessage(InformationMessage("IsAmmo: "..tostring(weapon.CurrentUsageItem.IsAmmo), Colors.Cyan))

        --     agents:Add(attacker)

        --     local center = collisionData.CollisionGlobalPosition
        --     local identity = Mat3.Identity
        --     local missileSpeed = weapon:GetModifiedMissileSpeedForCurrentUsage()

        --     for i = 1, split_amount, 1 do
        --         behavior:SetTimeout(50 * i, function ()
        --             local ydir = Vec3.One
        --             ydir:RotateAboutY(6.2831855 * MBRandom.RandomFloat)
        --             local dir = (attacker.Mission:GetRandomPositionAroundPoint(center, 5, 10, true) + ydir) - center
        --             dir:Normalize()
        --             identity.f = dir
        --             identity:Orthonormalize()
        --             attacker.Mission:AddCustomMissile(attacker, weapon, center, identity.f, identity, missileSpeed, missileSpeed + 50, false, missile.MissionObjectToIgnore, -1)
        --         end)
        --     end
        -- end,
        onAgentShootMissile = function (shooterAgent, weaponIndex, position, velocity, orientation, hasRigidBody, forcedMissileIndex)
            if onlyPlayer and not shooterAgent.IsMainAgent then
                return
            end

            local weapon = shooterAgent.Equipment[weaponIndex]
            if weapon == MissionWeapon.Invalid or weapon.CurrentUsageItem == nil then
                return
            end

            local missileSpeed = weapon:GetModifiedMissileSpeedForCurrentUsage()
            if weapon.AmmoWeapon ~= MissionWeapon.Invalid and weapon.AmmoWeapon.CurrentUsageItem ~= nil and weapon.AmmoWeapon.CurrentUsageItem.IsConsumable then
                weapon = weapon.AmmoWeapon
            end
            if not weapon.CurrentUsageItem.IsConsumable then
                return
            end

            local center = position
            local identity = Mat3.Identity
            local missionObjectToIgnore = SubModule:GetFirstMissionObject(shooterAgent.AgentVisuals:GetEntity())
            local step = 15.0 / split_amount
            local k = math.pi / 180

            for i = 1, split_amount, 1 do
                local angleRad = degreesToRadians(i * step)
                local dir = velocity:NormalizedCopy()
                dir:RotateAboutZ(-angleRad)
                identity.f = dir
                identity:Orthonormalize()
                mission:AddCustomMissile(shooterAgent, weapon, center, identity.f, identity, missileSpeed, missileSpeed + 20 * MBRandom.RandomFloat, hasRigidBody, missionObjectToIgnore, -1)
            end
            for i = 1, split_amount, 1 do
                local angleRad = degreesToRadians(i * step)
                local dir = velocity:NormalizedCopy()
                dir:RotateAboutZ(angleRad)
                identity.f = dir
                identity:Orthonormalize()
                mission:AddCustomMissile(shooterAgent, weapon, center, identity.f, identity, missileSpeed, missileSpeed + 20 * MBRandom.RandomFloat, hasRigidBody, missionObjectToIgnore, -1)
            end
        end
    })
    mission:AddMissionBehavior(behavior)
end
}